#include "messageboxwidget.h"
#include "ui_messageboxwidget.h"
#include<QDebug>
#include<QScreen>
MessageBoxWidget::MessageBoxWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MessageBoxWidget)
{
    ui->setupUi(this);
    isConfirm=false;
    setFixedSize(500,250);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowModality(Qt::ApplicationModal);
    setWindowIcon(QIcon(":/img/application.png"));
}

MessageBoxWidget::~MessageBoxWidget()
{
    delete ui;
}

void MessageBoxWidget::setMessageBox(const QString &text, MesType type,QWidget* parent)
{
    switch (type) {
    case Info:
    {
        ui->tipLabel->setText("提示");
        ui->tipLabel->setStyleSheet("color:rgb(85, 85, 255);"
                                    "font-family: 'Microsoft YaHei';"
                                    );
        break;
    }
    case Warning:
    {
        ui->tipLabel->setText("警告");
        ui->tipLabel->setStyleSheet("color:rgb(255, 85, 0);"
                                    "font-family: 'Microsoft YaHei';"
                                    );
        break;
    }
    case Error:
    {
        ui->tipLabel->setText("错误");
        ui->tipLabel->setStyleSheet("color:rgb(255, 0, 0);"
                                    "font-family: 'Microsoft YaHei';"
                                    );
        break;
    }
    default:
        break;
    }
    ui->tipText->setStyleSheet("font-family: 'Microsoft YaHei';"
                               "background-color: rgb(231, 249, 255);"
                               "border-radius:6px;");
    QFont font;
    font.setPointSize(20);
    font.setBold(true);
    ui->tipLabel->setFont(font);
    QFont font2;
    font2.setPointSize(10);
    font2.setBold(true);
    ui->tipText->setFont(font2);

    ui->tipText->setHtml("<div align='center'>"+text+"</div>");

    adjustSize();
    // 放置在父窗口正中上方
    if (parent) {
        int x = (parent->width() - width()) / 2;
        int y = 30; // 离顶部30px
        move(parent->mapToGlobal(QPoint(x, y)));
    } else {
        // 没有父窗口则屏幕中央上方
        QScreen* screen = QGuiApplication::primaryScreen();  // 获取主屏幕
        QRect screenRect = screen->availableGeometry();      // 可用屏幕区域（不包括任务栏等）
        move((screenRect.width() - width()) / 2, 30);
    }
}



void MessageBoxWidget::on_confirmBtn_clicked()
{
    isConfirm=true;
    close();
}


void MessageBoxWidget::on_cancelBtn_clicked()
{
    isConfirm=false;
    close();
}

void MessageBoxWidget::closeEvent(QCloseEvent *event)
{
    if(isConfirm){
        emit confirmSignal();
    }else{
        emit cancelSignal();
    }
    event->accept();
}
